package com.ruoyi.operator.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.ShiroUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.operator.constant.MemberConstants;
import org.apache.commons.lang3.RandomStringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.operator.mapper.OptShopMemberMapper;
import com.ruoyi.operator.domain.OptShopMember;
import com.ruoyi.operator.service.IOptShopMemberService;
import com.ruoyi.common.core.text.Convert;

/**
 * 商户会员Service业务层处理
 *
 * @author zhanghang
 * @date 2021-01-20
 */
@Service
public class OptShopMemberServiceImpl implements IOptShopMemberService
{
    @Autowired
    private OptShopMemberMapper optShopMemberMapper;

    /**
     * 查询商户会员
     *
     * @param id 商户会员ID
     * @return 商户会员
     */
    @Override
    public OptShopMember selectOptShopMemberById(Long id)
    {
        return optShopMemberMapper.selectOptShopMemberById(id);
    }

    /**
     * 查询商户会员
     *
     * @param memberCardNumber 商户会员ID
     * @return 商户会员
     */
    @Override
    public OptShopMember selectMemberByCardNumber(String memberCardNumber)
    {
        return optShopMemberMapper.selectMemberByCardNumber(memberCardNumber);
    }

    /**
     * 查询商户会员列表
     *
     * @param optShopMember 商户会员
     * @return 商户会员
     */
    @Override
    public List<OptShopMember> selectOptShopMemberList(OptShopMember optShopMember)
    {
        return optShopMemberMapper.selectOptShopMemberList(optShopMember);
    }

    /**
     * 查询商户会员数
     *
     * @param deptId 组织Id
     * @return 商户会员集合
     */
    @Override
    public int selectMemberCountByDept(Long deptId) {
        return optShopMemberMapper.selectMemberCountByDept(deptId);
    }

    /**
     * 新增商户会员
     *
     * @param optShopMember 商户会员
     * @return 结果
     */
    @Override
    public int insertOptShopMember(OptShopMember optShopMember)
    {
        String  number=  new SimpleDateFormat("yyyyMMddhhmmss").format(new Date())+ RandomStringUtils.randomNumeric(6);
        optShopMember.setMemberCardNumber(number);
        optShopMember.setCreateTime(DateUtils.getNowDate());
        SysUser currentUser  = (SysUser) SecurityUtils.getSubject().getPrincipal();
        optShopMember.setCreateBy(currentUser.getUserName());
        optShopMember.setDeptId(currentUser.getDept().getDeptId());
        optShopMember.setDeptName(currentUser.getDept().getDeptName());
        optShopMember.setIsDelete("0");
        return optShopMemberMapper.insertOptShopMember(optShopMember);
    }

    /**
     * 修改商户会员
     *
     * @param optShopMember 商户会员
     * @return 结果
     */
    @Override
    public int updateOptShopMember(OptShopMember optShopMember)
    {
        optShopMember.setUpdateTime(DateUtils.getNowDate());
        return optShopMemberMapper.updateOptShopMember(optShopMember);
    }

    /**
     * 删除商户会员对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteOptShopMemberByIds(String ids)
    {
        return optShopMemberMapper.deleteOptShopMemberByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除商户会员信息
     *
     * @param id 商户会员ID
     * @return 结果
     */
    @Override
    public int deleteOptShopMemberById(Long id)
    {
        return optShopMemberMapper.deleteOptShopMemberById(id);
    }

    /**
     * 校验相同商户下手机号码是否唯一
     *
     * @param optShopMember 会员信息
     * @return 结果
     */
    @Override
    public String checkPhoneUnique(OptShopMember optShopMember){
        Long deptId = StringUtils.isNull(optShopMember.getDeptId()) ? -1L : optShopMember.getDeptId();
        SysUser currentUser = ShiroUtils.getSysUser();
        optShopMember.setDeptId(currentUser.getDeptId());
        OptShopMember info = optShopMemberMapper.checkPhoneUnique(optShopMember);
        if (StringUtils.isNotNull(info) && info.getDeptId().longValue() != deptId.longValue())
        {
            return UserConstants.USER_PHONE_NOT_UNIQUE;
        }
        return UserConstants.USER_PHONE_UNIQUE;
    }

    /**
     * 校验会员余额是否为 0
     *
     * @param optShopMember 会员信息
     * @return 结果
     */
    @Override
    public String checkBalanceIsZero(OptShopMember optShopMember) {
        BigDecimal balance = optShopMemberMapper.selectBalanceById(optShopMember.getId());
        if(balance.compareTo(BigDecimal.ZERO)==0){
            return MemberConstants.MEMBER_BALANCE_ZERO;
        }
        return MemberConstants.MEMBER_BALANCE_NOT_ZERO;
    }

    @Override
    public int updateByIds(OptShopMember optShopMember) {
        return optShopMemberMapper.updateByIds(optShopMember);
    }

    @Override
    public int SelectMobileNumber(OptShopMember optShopMember) {
        return optShopMemberMapper.SelectMobileNumber(optShopMember);
    }

    @Override
    public int updateOptShopMemberinfo(OptShopMember optShopMember) {
        return optShopMemberMapper.updateOptShopMemberinfo(optShopMember);
    }

}
